package com.genersoft.iot.vmp.storager.dao;

import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用于存储上级平台
 */
@Mapper
@Repository
public interface ParentPlatformMapper {

    @Insert(value = {"<script> INSERT INTO parent_platform (id,enable, name, server_gb_id, server_gb_domain, server_ip, server_port, device_gb_id, device_ip,  " +
            "            device_port, username, password, expires, keep_timeout, transport, character_set, ptz, rtcp, " +
            "            status) " +
            "            VALUES (seq_parent_platform.nextval,"+
            "<if test=\"enable==true\"> 1 ,</if> "+
            "<if test=\"enable!=true\"> 0 ,</if> "+
            "'${name}', '${serverGBId}', '${serverGBDomain}', '${serverIP}', ${serverPort}, '${deviceGBId}', '${deviceIp}', " +
            "            '${devicePort}', '${username}', '${password}', '${expires}', '${keepTimeout}', '${transport}', '${characterSet}', "+
            "<if test=\"ptz==true\"> 1 ,</if> "+
            "<if test=\"ptz!=true\"> 0 ,</if> "+
            "<if test=\"rtcp==true\"> 1 ,</if> "+
            "<if test=\"rtcp!=true\"> 0 ,</if> "+
            "<if test=\"status==true\"> 1 </if> "+
            "<if test=\"status!=true\"> 0 </if> "+
   			")</script>"})
    int addParentPlatform(ParentPlatform parentPlatform);

    @Update(value = {"<script> UPDATE parent_platform set  " +
    		"<if test=\"enable != null and enable==true\"> enable=1 ,</if> "+
            "<if test=\"enable != null and enable!=true\"> enable=0 ,</if> "+
            "name=#{name}," +
            "device_gb_id=#{deviceGBId}," +
            "server_gb_id=#{serverGBId}, " +
            "server_gb_domain=#{serverGBDomain}, " +
            "server_ip=#{serverIP}," +
            "server_port=#{serverPort}, " +
            "device_ip=#{deviceIp}, " +
            "device_port=#{devicePort}, " +
            "username=#{username}, " +
            "password=#{password}, " +
            "expires=#{expires}, " +
            "keep_timeout=#{keepTimeout}, " +
            "transport=#{transport}, " +
            "character_set=#{characterSet}, " +
            "<if test=\"ptz != null and ptz==true\"> ptz=1 ,</if> "+
            "<if test=\"ptz != null and ptz!=true\"> ptz=0 ,</if> "+
            "<if test=\"rtcp != null and rtcp==true\"> rtcp=1 ,</if> "+
            "<if test=\"rtcp != null and rtcp!=true\"> rtcp=0 ,</if> "+
            "<if test=\"status != null and status==true\"> status=1 ,</if> "+
            "<if test=\"status != null and status!=true\"> status=0 ,</if> "+
            "WHERE id=#{id}"+
    		"</script>"})
    int updateParentPlatform(ParentPlatform parentPlatform);

    @Delete("DELETE FROM parent_platform WHERE server_gb_id=#{serverGBId}")
    int delParentPlatform(ParentPlatform parentPlatform);

    @Select("SELECT *, ((SELECT count(0)\n" +
            "              FROM platform_gb_channel pc\n" +
            "              WHERE pc.platform_id = pp.server_gb_id)\n" +
            "              +\n" +
            "              (SELECT count(0)\n" +
            "              FROM platform_gb_stream pgs\n" +
            "              WHERE pgs.platform_id = pp.server_gb_id)) as channelCount\n" +
            "FROM parent_platform pp ")
    @ResultMap("parentPlatform")
    @Result(property = "channelCount", column = "channelCount")
    List<ParentPlatform> getParentPlatformList();

    @Select(value = {"<script> SELECT * FROM parent_platform WHERE "+
    		"<if test=\"status != null and status==true\"> status=1 </if> "+
            "<if test=\"status != null and status!=true\"> status=0 </if> "+
    "</script>"})
    @Results(id="parentPlatform",
    value = { 
       @Result(property = "id", column = "id"), @Result(property = "enable", column = "enable"), 
       @Result(property = "name", column = "name"),@Result(property = "serverGBId", column = "server_gb_id"),
       @Result(property = "serverGBDomain", column = "server_gb_domain"),  @Result(property = "serverIP", column = "server_ip"), 
       @Result(property = "serverPort", column = "server_port"), @Result(property = "deviceGBId", column = "device_gb_id"), 
       @Result(property = "deviceIp", column = "device_ip"), @Result(property = "devicePort", column = "device_port"), 
       @Result(property = "username", column = "username"), @Result(property = "password", column = "password"), 
       @Result(property = "expires", column = "expires"), @Result(property = "keepTimeout", column = "keep_timeout"), 
       @Result(property = "transport", column = "transport"), @Result(property = "characterSet", column = "character_set"), 
       @Result(property = "ptz", column = "ptz"), @Result(property = "rtcp", column = "rtcp"), 
       @Result(property = "status", column = "status"), 
       })
    List<ParentPlatform> getEnableParentPlatformList(boolean enable);

    @Select("SELECT * FROM parent_platform WHERE server_gb_id=#{platformGbId}")
    @ResultMap("parentPlatform")
    ParentPlatform getParentPlatByServerGBId(String platformGbId);

    @Select("SELECT * FROM parent_platform WHERE id=#{id}")
    @ResultMap("parentPlatform")
    ParentPlatform getParentPlatById(int id);

    @Update("UPDATE parent_platform SET status=0" )
    int outlineForAllParentPlatform();

    @Update(value = {"<script> UPDATE parent_platform SET <if test=\"online == true\">status=1</if><if test=\"online!=true\">status=0</if>  WHERE server_gb_id=#{platformGbID} </script>" })
    int updateParentPlatformStatus(String platformGbID, boolean online);
}
